package com.crm.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.domain.dto.QueryContract;
import com.crm.domain.entity.Contract;
import com.crm.mapper.*;
import com.crm.service.ContractService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements ContractService {

    private final ContractMapper contractMapper;
    private final DeptMapper deptMapper;
        private final CustonmersMapper custonmersMapper;
    private final CourseMapper courseMapper;
    private final ActivityMapper activityMapper;
    /**
     * 分页查询合同列表
     *
     * @param queryContract
     * @return
     */
    @Override
    public Page<Contract> selectByPage(QueryContract queryContract) {
        //1.设置分页参数
        /*
            请求list和请求分页都是同一个接口 如果不对空值进行初始化 会有空指针异常。
            解决方式:手动设置初始值 但ObjectUtil.isEmpty会把null也判断为对象 需要取值或者queryDictType==null
         */
        Page<Contract> page = ObjectUtil.isEmpty(queryContract.getPageNum())? Page.of(1, 10):Page.of(queryContract.getPageNum(), queryContract.getPageSize());

        //2.调用Mapper查询数据
        //2.1构建查询条件
        Map<String, Object> params = queryContract.getParams();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        LocalDate beginCreateTime =ObjectUtil.isEmpty(params.get("beginCreateTime"))?null:(LocalDate.parse((String)params.get("beginCreateTime"), formatter));
        LocalDate endCreateTime =ObjectUtil.isEmpty(params.get("endCreateTime"))?null:(LocalDate.parse((String)params.get("endCreateTime"), formatter));

        LambdaQueryWrapper<Contract> wrapper = Wrappers.<Contract>lambdaQuery()
                .eq(ObjectUtil.isNotEmpty(queryContract.getContractNo()),Contract::getContractNo, queryContract.getContractNo())
                .like(ObjectUtil.isNotEmpty(queryContract.getName()),Contract::getName, queryContract.getName())
                .eq(ObjectUtil.isNotEmpty(queryContract.getPhone()),Contract::getPhone, queryContract.getPhone())
                .eq(ObjectUtil.isNotEmpty(queryContract.getCourseId()),Contract::getCourseId, queryContract.getCourseId())
                .eq(ObjectUtil.isNotEmpty(queryContract.getSubject()),Contract::getSubject, queryContract.getSubject())
                .between(ObjectUtil.isNotEmpty(beginCreateTime)&&ObjectUtil.isNotEmpty(endCreateTime)
                        ,Contract::getCreateTime, beginCreateTime, endCreateTime);

        //2.2用m-plus查询
        Page<Contract> contractPage = page(page, wrapper);

        return contractPage;
    }

}
